package com.xhwl.data.config;

import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.service.IAppService;
import com.xhwl.data.util.SpringContextUtils;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 下午3:15 2022/7/19
 */
@Component
public class WebAppArgResolver implements WebArgumentResolver {

    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest){
        if (null != methodParameter.getParameterType() && methodParameter.getParameterType().equals(App.class)) {
            HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
            //来自网关解析token后，往请求头新增的userId
            String appId = request.getHeader("appId");
            if(StringUtils.isEmpty(appId)){
                return null;
            }
            IAppService appCentService = SpringContextUtils.getBean(IAppService.class);
            return appCentService.getByAppId(appId);
        }
        return UNRESOLVED;
    }
}
